/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 073. 狒狒吃香蕉
 * @author: WXY
 * @create: 2023-01-10 09:43
 * @Version 1.0
 **/
public class offer_twentyfour3_minEatingSpeed {
    public static int minEatingSpeed(int[] arr, int h) {
        int l = 1, r = getMax(arr);
        int ans = r;
        while (l < r) {
            int speed = l + (r - l) / 2;
            long time = getTime(arr, speed);
            if (time <= h) {
                ans = speed;
                r = speed;
            } else {
                l = speed + 1;
            }
        }
        return ans;
    }

    private static long getTime(int[] arr, int speed) {
        long time = 0;
        for (int num : arr) {
            int curTime = (num + speed - 1) / speed;
            time += curTime;
        }
        return time;
    }

    private static int getMax(int[] arr) {
        int max = arr[0];
        for (int n : arr) {
            max = Math.max(max, n);
        }
        return max;
    }
}
